package com.example.config;

import purejavacomm.CommPort;
import purejavacomm.CommPortIdentifier;
import purejavacomm.SerialPort;

import java.io.IOException;

import java.io.InputStream;
import java.io.OutputStream;


/**
 * @Auther: maxiaoyu
 * @Description: 1.列举出本地所有的串口;
 * 2.打开所有串口(但是未向串口中写数据);
 * 3.关闭打开的串口。
 * @Date: 2019/9/10 14:36
 */
public class SerialComm4MainWindows {
    public SerialComm4MainWindows() {
        super();
    }


    public void connect(String portName) throws Exception {
        CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
        if (portIdentifier.isCurrentlyOwned()) {
            System.out.println("Error: Port is currently in use");
        } else {
            CommPort commPort = portIdentifier.open(this.getClass().getName(), 2000);
            if (commPort instanceof SerialPort) {
                SerialPort serialPort = (SerialPort) commPort;
                serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);


                InputStream in = serialPort.getInputStream();
                (new Thread(new SerialReader(in))).start();
            } else {
                System.out.println("Error: Only serial ports are handled by this example.");
            }
        }
    }


    /**
     * 读串口数据
     */

    public static class SerialReader implements Runnable {
        InputStream in;

        public SerialReader(InputStream in) {
            this.in = in;
        }

        public void run() {
            byte[] buffer = new byte[1024];
            int len = -1;
            try {
                while ((len = this.in.read(buffer)) > -1) {
                    String getValue = new String(buffer, 0, len);
                    if (getValue != null && getValue.length() > 2) {
                        System.out.println("未裁剪："+getValue);
                        getValue = getValue.replaceAll("\u0002","").replaceAll("\u0003","");
                        String[] strings = getValue.split("\\+");
                        for (int i = 1; i < strings.length; i++) {
                            if (strings[i].length() < 8) {
                                System.out.println(strings[i]);
                            } else {
                                strings[i] = strings[i].substring(0, 6);
                            }
                            System.out.println("getValue:" + strings[i]);
                            WebSocketServer.sendInfo(strings[i],null);//全部发送
                        }
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static class SerialWriter implements Runnable {
        OutputStream out;

        public SerialWriter(OutputStream out) {
            this.out = out;
        }

        public void run() {
            try {
                int c = 0;
                while ((c = System.in.read()) > -1) {
                    this.out.write(c);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
